package com.atbun.eduservice.controller;


import com.atbun.commonutils.R;
import com.atbun.eduservice.entity.EduTeacher;
import com.atbun.eduservice.entity.vo.TeacherQuery;
import com.atbun.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-01
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher/")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;


    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id){

        boolean remove = this.eduTeacherService.removeById(id);
        if (remove){
            return R.ok();
//            return ResultObj.DELETE_SUCCESS;
    }
        return R.error();
//        return ResultObj.DELETE_ERROR;
    }

    @ApiOperation(value = "分页讲师列表")
    @PostMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
            @RequestBody(required = false) TeacherQuery teacherQuery){

        Page<EduTeacher> pageParam = new Page<>(page,limit);
        this.eduTeacherService.pageQuery(pageParam,teacherQuery); //第二个参数是QueryWrapper条件,后续可以把分页跟条件查询合并。
        return R.ok().data("total",pageParam.getTotal()).data("rows",pageParam.getRecords());
    }

    @ApiOperation(value = "添加讲师模块")
    @PostMapping("insert")
    public R insert(@RequestBody EduTeacher eduTeacher){

        boolean save = this.eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("item", teacher);
    }

    @ApiOperation(value = "根据id修改讲师信息")
    @PostMapping("updateById/{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id,
            @ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher){
//        根据id查询是否存在该讲师
        this.getById(id);
        eduTeacher.setId(id);
        boolean updateTeacher = this.eduTeacherService.updateById(eduTeacher);
        if (updateTeacher){
            return R.ok();
        }
        return R.error();
    }
}

